<template>
	<view class="withdrawal-wrapper">
		<view class="money-wrapper">
			<div class="list" v-if="bankCard===''">
				<div class="list-left">暂无银行卡</div>
			</div>
			<div class="list" v-else>
				<div class="list-left">收款：{{bankCard.bankName}}（{{bankCard.cardNo}}）</div>
			</div>
		</view>
		<div class="withdrawal-money">
			<div class="title">提现焕贝金额</div>
			<u-input class="input" :custom-style="customStyle" :border="false" v-model="form.money" type="digit"
				placeholder="请输入提现焕贝金额" placeholder-style="font-size:16px" @input="calculation" />
			<div class="tip" v-if="hbPriceTip">金额格式不正确</div>
			<div class="fee" v-if="isshow == 1 && form.money != ''">
				对应现金额(元)：{{ surplusSum }} 折扣率：{{ fee }}
			</div>
		</div>
		<div class="pay-wallet">
			<div class="title">付款钱包</div>
			<u-radio-group :wrap='true' v-model="form.owner" :active-color="lightColor" style="clear: both;"
				@change="walletChange">
				<u-radio v-if="userInfo.haveStore == true" name="SALE">
					<div class="wallet-list">
						<div class="name">销售钱包：{{ flowpriceSum }}</div>
						<div class="all" @click="whole(flowpriceSum)" v-if="flowpriceSum > 0">全部提现</div>
					</div>
				</u-radio>
				<u-radio v-if="userInfo.partnerType == 4" name="PROMOTE_FW">
					<div class="wallet-list">
						<div class="name">推广钱包服务费：{{ tuiguangSum }}</div>
						<div class="all" @click="whole(tuiguangSum)" v-if="tuiguangSum > 0">全部提现</div>
					</div>
				</u-radio>
				<u-radio v-if="userInfo.partnerType == 4" name="PROMOTE_HY">
					<div class="wallet-list">
						<div class="name">推广钱包会员费：{{ tuiguanghuiyuan }}</div>
						<div class="all" @click="whole(tuiguanghuiyuan)" v-if="tuiguanghuiyuan > 0">全部提现</div>
					</div>
				</u-radio>
				<u-radio v-if="userInfo.partnerType == 1 || userInfo.partnerType == 2" name="PROMOTE">
					<div class="wallet-list">
						<div class="name">推广钱包：{{ tuiguangSum }}</div>
						<div class="all" @click="whole(tuiguangSum)" v-if="tuiguangSum > 0">全部提现</div>
					</div>
				</u-radio>
			</u-radio-group>
		</div>
		<div class="button-wrapper">
			<button class="button" @click="surewith">确认提现</button>
			<div class="tansfer-history" @click="record">
				<span class="text">提现记录</span>
				<u-icon name="arrow-right" color="#999999"></u-icon>
			</div>
		</div>
		<pay-password @passwordFinish="passwordFinish" ref="payPassword"></pay-password>
		<u-popup v-model="show" mode="center" :mask-close-able="false" border-radius="14">
			<div class="chexiao-wrapper">
				<div class="title">系统提示</div>
				<text class="sub-title">【提现】需先绑定银行卡</text>
				<view class="buttons-wrapper">
					<div class="button cancel" @click="cancle">取消</div>
					<div class="button sure" @click="sure">去绑定</div>
				</view>
			</div>
		</u-popup>
		<u-popup v-model="phoneAuthPopup" mode="center" border-radius="16" :mask-close-able="true">
			<div class='withdraw-tip'>
				<div class="title">不能提现</div>
				<div class="tips">当前店铺暂无合同授权提现折扣</div>
				<div class="mobile">请联系管理员</div>
				<div class="mobile" @click="daphone('15545040068')">
					<u-icon name="phone-fill" color="#6e98fa"></u-icon> 15545040068
				</div>
				<div class="mobile" @click="daphone('0451-58567855')">
					<u-icon name="phone-fill" color="#fa9d6e"></u-icon> 0451-58567855
				</div>
			</div>
		</u-popup>
	</view>
</template>

<script>
	import {
		getCountSum,
		calculationwith,
		withdrawApply,
		calculatuiguang,
		withdrawtixian,
		withdrawalkoushui
	} from "@/api/withdrawal.js";
	import {
		getBankCard
	} from '@/api/invoiceAndBankCard'
	import storage from "@/utils/storage.js"
	import payPassword from "@/components/pay-password/pay-password.vue"
	import BigNumber from 'bignumber.js'
	export default {
		components: {
			payPassword
		},
		data() {
			return {
				// 提交请求状态识别变量
				requestStatus: false,
				show: false,
				bankCard: '',
				flowpriceSum: 0, // 销售钱包余额
				hbPriceTip: 0,
				surplusSum: '', // 对应人民币
				fee: '', // 对应折扣
				isshow: 0,
				lightColor: this.$lightColor,
				form: {
					money: '',
					owner: ''
				},
				userInfo: {},
				tuiguangSum: 0,
				tuiguanghuiyuan: 0,
				phoneAuthPopup: false, //提现弹窗
			}
		},
		onShow() {
			this.userInfo = storage.getUserInfo()
			if (this.userInfo.partnerType == 4) {
				this.chengtuifu()
				this.chengtuihui()
			} else {
				this.gettuiguang()
			}
			this.getwithdrawApply()
			this.getBankCard()
		},
		onLoad() {

		},
		methods: {
			getBankCard() {
				// 获取银行卡信息
				getBankCard().then((res) => {
					if (res.statusCode == 200 && res.data.result && res.data.result.length > 0) {
						this.show = false
						let card = res.data.result[0]
						card.cardNo = card.cardNo.substr(-4)
						this.bankCard = res.data.result[0]
					} else {
						this.show = true
						this.bankCard = ''
					}
				});
			},
			// 切换钱包，选择钱包
			walletChange() {
				if (this.form.owner == 'SALE') {
					if (this.userInfo.haveStore && (this.userInfo.isHaveContract === null || this.userInfo
							.isHaveContract === '0')) {
						this.phoneAuthPopup = true
						this.form.owner = ''
						return
					}
				}

				this.calculation()
			},
			// 计算现金额
			calculation() {
				if (!this.$u.test.amount(this.form.money)) {
					this.isshow = 0
					uni.showToast({
						title: "焕贝金额格式不正确！",
						duration: 2000,
						icon: "none",
					});
					return
				}
				if (this.form.owner == '') {
					this.isshow = 0
					uni.showToast({
						title: "请选择要提现的钱包！",
						duration: 2000,
						icon: "none",
					});
					return
				}
				let parse = {
					price: this.form.money,
					owner: this.form.owner
				}
				calculatuiguang(parse).then((res) => {
					if (res.data && res.data.code == 200) {
						this.surplusSum = Math.floor(res.data.result.money * 100) / 100
						this.fee = Math.floor(res.data.result.fee * 100) / 100
						this.hbPriceTip = 0
						this.isshow = 1
					} else {
						this.hbPriceTip = 1
						this.isshow = 0
					}
				});
			},
			// 查询销售钱包余额
			getwithdrawApply() {
				let parse = {
					owner: 'SALE'
				}
				getCountSum(parse).then((res) => {
					if (res.data.code == 200) {
						this.flowpriceSum = new BigNumber(res.data.result.memberWallet).dp(2, BigNumber.ROUND_DOWN)
							.toNumber()
					}
				});
			},
			// 查询推广钱包余额
			gettuiguang() {
				let parse = {
					owner: 'PROMOTE'
				}
				getCountSum(parse).then((res) => {
					if (res.data.code == 200) {
						this.tuiguangSum = new BigNumber(res.data.result.memberWallet).dp(2, BigNumber.ROUND_DOWN)
							.toNumber()
					}
				});
			},
			// 查询推广钱包服务费
			chengtuifu() {
				let parse = {
					owner: 'PROMOTE_FW'
				}
				getCountSum(parse).then((res) => {
					if (res.data.code == 200) {
						this.tuiguangSum = new BigNumber(res.data.result.memberWallet).dp(2, BigNumber.ROUND_DOWN)
							.toNumber()
					}
				});
			},
			// 查询推广钱包会员费
			chengtuihui() {
				let parse = {
					owner: 'PROMOTE_HY'
				}
				getCountSum(parse).then((res) => {
					if (res.data.code == 200) {
						this.tuiguanghuiyuan = new BigNumber(res.data.result.memberWallet).dp(2, BigNumber
							.ROUND_DOWN).toNumber()
					}
				});
			},
			// 全部提现
			whole(money) {
				this.form.money = money
				setTimeout(() => {
					this.calculation()
				}, 1000)
			},
			// 确认提现 --start--
			surewith() {
				let that = this
				if (this.form.owner == '') {
					uni.showToast({
						title: "请选择要提现的钱包！",
						duration: 2000,
						icon: "none",
					});
				} else if (this.isshow == 0) {
					uni.showToast({
						title: "请输入正确的焕贝金额",
						duration: 2000,
						icon: "none",
					});
				} else if (!(this.surplusSum > 0)) {
					uni.showToast({
						title: "提现金额小于【0.01】元，请重新输入！",
						duration: 2000,
						icon: "none",
					});
				} else {
					let owner = this.form.owner
					let money = that.form.money
					let haveMoney = 0
					if (owner === 'SALE') {
						haveMoney = this.flowpriceSum
					} else if (owner === 'PROMOTE_FW') {
						haveMoney = this.tuiguangSum
					} else if (owner === 'PROMOTE_HY') {
						haveMoney = this.tuiguanghuiyuan
					} else if (owner === 'PROMOTE') {
						haveMoney = this.tuiguangSum
					}
					if (new BigNumber(haveMoney).comparedTo(money) === -1) {
						uni.showToast({
							title: '该钱包余额不足',
							icon: 'none'
						})
						return
					}
					that.$refs.payPassword.showPopup()
				}
			},
			passwordFinish(word) {
				this.surewithsss(this.form, word)
			},
			surewithsss(form, word) {
				if (this.form.owner == '') {
					this.isshow = 0
					uni.showToast({
						title: "请选择要提现的钱包！",
						duration: 2000,
						icon: "none",
					});
					return
				}
				let parse = {
					price: this.form.money,
					owner: this.form.owner,
					payPassword: word,
					cardId: this.bankCard.id
				}
				// 提交防抖 只执行一次
				if (this.requestStatus) {
					// 利用 return 终止函数继续运行
					return false;
				}
				// 执行函数
				this.requestStatus = true;
				setTimeout(() => {
					withdrawtixian(parse).then((res) => {
						if (res.data.code == 200) {
							uni.redirectTo({
								url: "/pages/mine/withdrawal/withsuss?" +
									"createTime=" + res.data.result.createTime +
									"&bank=" +
									`${this.bankCard.bankName}（${this.bankCard.cardNo})` +
									"&payPrice=" + res.data.result.applyMoney +
									"&money=" + this.form.money +
									"&txsn=" + res.data.result.sn +
									"&handlingMoney=" + res.data.result.handlingMoney +
									"&owner=" + this.form.owner +
									"&withholdingAdvance=" + res.data.result.withholdingAdvance
							})
						}
					});
					// 模拟执行完毕
					// 改变 requestStatus
					this.requestStatus = false;
				}, 2000)
			},
			// 确认提现 --end--
			// 记录
			record() {
				uni.navigateTo({
					url: '/pages/mine/withdrawal/record?owner=' + 'all'
				})
			},
			// 银行卡提示 关闭
			cancle() {
				uni.navigateBack()
			},
			// 银行卡提示 确定
			sure() {
				uni.navigateTo({
					url: '/pages/mine/bankCard/bankCard'
				})
			}
		}
	}
</script>

<style lang="scss">
	.withdrawal-wrapper {
		.money-wrapper {
			margin-top: 24upx;

			.list {
				padding: 36upx;
				display: flex;
				justify-content: center;
				align-items: center;
				background-color: #fff;

				.list-left {
					flex: 8;
				}
			}
		}

		.withdrawal-money {
			margin-top: 24upx;
			padding: 24upx 32upx;
			background-color: #ffffff;

			.title {
				font-weight: bold;
				margin-bottom: 24upx;
			}

			.tip {
				color: #ff3437;
				font-size: 24upx;
			}

			.fee {
				margin-top: 24upx;
			}
		}

		.pay-wallet {
			margin-top: 24upx;
			padding: 24upx 32upx;
			background-color: #ffffff;

			.title {
				font-weight: bold;
				margin-bottom: 24upx;
			}

			.wallet-list {
				width: 600rpx;
				display: flex;
				justify-content: space-between;

				.all {
					font-size: 24upx;
					color: #e3342b;
				}
			}
		}

		.button-wrapper {
			margin-top: 100upx;
			display: flex;
			flex-direction: column;
			align-items: center;

			.button {
				width: 320upx;
				height: 80upx;
				line-height: 80upx;
				background: linear-gradient(90deg, #fa9d6e 0%, #e3342b 100%);
				border-radius: 40upx;
				color: #ffffff;
				font-size: 28upx;
			}

			.tansfer-history {
				width: 200upx;
				margin-top: 16upx;
				padding: 26upx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				color: #999999;

			}
		}

		.cash {
			margin-top: 15upx;
		}

		.withdraw-tip {
			padding: 80upx;
			text-align: center;

			.title {
				font-size: 32upx;
			}

			.tips {
				margin: 40upx 0 60upx 0;
			}

			.mobile {
				margin-top: 24upx;
				display: flex;
				justify-content: center;
			}
		}
	}

	/deep/ .chexiao-wrapper {
		padding: 40upx;
		text-align: center;

		.title {
			margin: 24upx;
			font-size: 32upx;
			font-weight: bold;
		}

		.sub-title {}

		.buttons-wrapper {
			margin-top: 60upx;
			display: flex;
			justify-content: space-between;

			.button {
				width: 44%;
				height: 60upx;
				line-height: 60upx;
				text-align: center;
				border-radius: 72upx;
			}

			.sure {
				background-color: #FF946E;
				color: #ffffff;
			}

			.cancel {
				border: 2upx solid #aaaaaa;
			}
		}
	}
</style>
